<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;

class UserRoleTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //初始化添加一个默认用户以及一个超级管理员角色
        $params = [
            'username' => 'admin@admin.com',
            'email' => 'admin@admin.com',
            'actived' => 1,
            'password' => Hash::make('admin@admin.com'),
            'avatar' => 'http://pjjvscyez.bkt.clouddn.com/2018-12-11-04-10-02/8bf696875b8ff42e0eb490e5bc0d2e8e.jpg'
        ];

        DB::beginTransaction();

        $user = \App\Models\Users::create($params);

        if (!$user) die('创建默认用户失败');

        $role = [
            'name' => 'super_admin',
            'guard_name' => 'web',
            'description' => '超级管理员'
        ];


        $role = Role::create($role);
        if (!$role) die('创建默认角色失败');

        //添加默认角色到默认用户
        $res = $user->assignRole($role->name);
        if (!$res) die('初始化用户失败');

        DB::commit();
       echo '初始化用户成功' . PHP_EOL . '默认用户名：admin' . PHP_EOL . '默认密码：admin@admin.com';exit;
    }
}
